package com.example.ordersystem.utils;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.example.ordersystem.pojo.Food;
import com.example.ordersystem.pojo.Store;
import com.example.ordersystem.pojo.User;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import static android.content.Context.MODE_APPEND;//数据追加
import static android.content.Context.MODE_PRIVATE;//新数据覆盖旧数据，旧数据清空

/*
 * 存储工具类   用简单存储sharedperferences来创建一个文件来存储相关的数据
 * 这里存储了6个文件
 * private static SharedPreferences sp;  用户信息存取
 * private static SharedPreferences sp1; 用户登录的标识
 * private static SharedPreferences sp2; 店铺信息存取
 * private static SharedPreferences sp3; 菜单信息存取
 * private static SharedPreferences sp4; 用户收藏信息存取
 * private static SharedPreferences sp5; 用户订单信息存取
 */
public class ListDataSave {

    private static SharedPreferences sp;
    private static SharedPreferences sp1;
    private static SharedPreferences sp2;
    private static SharedPreferences sp3;
    private static SharedPreferences sp4;
    private static SharedPreferences sp5;

    //存储用户数据
    @SuppressLint("WrongConstant")
    public static void putUserBean(Context ctx, List<User> userList, String key) {
        if (sp == null) {
            sp = ctx.getSharedPreferences("config_user", MODE_APPEND);
        }
        SharedPreferences.Editor editor = sp.edit();
        Gson gson = new Gson();
        String json = gson.toJson(userList);
        editor.putString(key, json);
        editor.commit();
    }

    //取出用户数据
    @SuppressLint("WrongConstant")
    public static List<User> getUserList(Context ctx, String key) {
        if (sp == null) {
            sp = ctx.getSharedPreferences("config_user", MODE_APPEND);
        }
        Gson gson = new Gson();
        String json = sp.getString(key, null);
        Type type = new TypeToken<List<User>>() {
        }.getType();
        List<User> arrayList = gson.fromJson(json, type);
        return arrayList;
    }


    //存储登录的用户名  用于判断用户是否登录，如果用户退出则存储信息为“自定义”
    private static SharedPreferences getSp(Context context) {
        if (sp1 == null) {
            sp1 = context.getSharedPreferences("config_userLogin", Context.MODE_PRIVATE);
        }
        return sp1;
    }

    public static void putUserLogin(Context context, String value) {
        SharedPreferences preferences = getSp(context);
        //存入数据
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("userLogin", value);
        editor.commit();
    }


    public static String getUserLogin(Context context) {
        SharedPreferences preferences = getSp(context);
        return preferences.getString("userLogin", "LoginOut");
    }


//店铺饭店的存储

    public static void putStoreBean(Context ctx, List<Store> storeList) {
        if (sp2 == null) {
            sp2 = ctx.getSharedPreferences("config_store", MODE_PRIVATE);
        }
        SharedPreferences.Editor editor = sp2.edit();
        Gson gson = new Gson();
        String json = gson.toJson(storeList);
        editor.putString("store", json);
        editor.commit();
    }

    //店铺信息的取出
    public static List<Store> getStoreList(Context ctx) {
        if (sp2 == null) {
            sp2 = ctx.getSharedPreferences("config_store", MODE_PRIVATE);
        }
        Gson gson = new Gson();
        String json = sp2.getString("store", null);
        Type type = new TypeToken<List<Store>>() {
        }.getType();
        List<Store> arrayList = gson.fromJson(json, type);
        return arrayList;
    }


    //存储菜单信息
    public static void putMenuBean(Context ctx, List<Food> foodList, String key) {
        if (sp3 == null) {
            sp3 = ctx.getSharedPreferences("config_menu", MODE_PRIVATE);
        }
        SharedPreferences.Editor editor = sp3.edit();
        Gson gson = new Gson();
        String json = gson.toJson(foodList);
        editor.putString(key, json);
        editor.commit();
    }

    //取出菜单信息
    public static List<Food> getMenuList(Context ctx, String key) {
        if (sp3 == null) {
            sp3 = ctx.getSharedPreferences("config_menu", MODE_PRIVATE);
        }
        Gson gson = new Gson();
        String json = sp3.getString(key, null);
        Type type = new TypeToken<List<Food>>() {
        }.getType();
        List<Food> arrayList = gson.fromJson(json, type);
        return arrayList;
    }


    //存储用户对应的喜爱的菜品

    @SuppressLint("WrongConstant")
    public static void putUserFoodLike(Context ctx, HashMap<String, List<Food>> map, String key) {
        if (sp4 == null) {
            sp4 = ctx.getSharedPreferences("config_user_food_like", MODE_APPEND);
        }
        SharedPreferences.Editor editor = sp4.edit();
        Gson gson = new Gson();
        String json = gson.toJson(map);
        editor.putString(key, json);
        editor.commit();
    }

    //取出用户对应喜欢的菜品
    @SuppressLint("WrongConstant")
    public static HashMap<String, List<Food>> getUserFoodLikeList(Context ctx, String key) {
        if (sp4 == null) {
            sp4 = ctx.getSharedPreferences("config_user_food_like", MODE_APPEND);
        }
        Gson gson = new Gson();
        String json = sp4.getString(key, null);
        Type type = new TypeToken<HashMap<String, List<Food>>>() {
        }.getType();
        HashMap<String, List<Food>> map = gson.fromJson(json, type);
        return map;
    }


    //用户订单菜品信息
    @SuppressLint("WrongConstant")
    public static void putUserFoodOrder(Context ctx, HashMap<String, List<Food>> map, String key) {
        if (sp5 == null) {
            sp5 = ctx.getSharedPreferences("config_user_food_order", MODE_APPEND);
        }
        SharedPreferences.Editor editor = sp5.edit();
        Gson gson = new Gson();
        String json = gson.toJson(map);
        editor.putString(key, json);
        editor.commit();
    }

    //取出订单信息
    @SuppressLint("WrongConstant")
    public static HashMap<String, List<Food>> getUserFoodOrderList(Context ctx, String key) {
        if (sp5 == null) {
            sp5 = ctx.getSharedPreferences("config_user_food_order", MODE_APPEND);
        }
        Gson gson = new Gson();
        String json = sp5.getString(key, null);
        Type type = new TypeToken<HashMap<String, List<Food>>>() {
        }.getType();
        HashMap<String, List<Food>> map = gson.fromJson(json, type);
        return map;
    }

}

